<?php
global $db;

if(in_array(0, $arr_orders_id)) 
    $sql_orders = "";
else
    $sql_orders = " and orders_id in (" . implode(',', $arr_orders_id) . ")";
        
$sql = "SELECT orders_id FROM ".TABLE_ORDERS." WHERE orders_status = ".$orderStatus . " and delivery_country = N'España'" . $sql_orders;
$pending_orders = $db->Execute($sql);

while (!$pending_orders->EOF) {

$oID = zen_db_prepare_input($pending_orders->fields['orders_id']);
$order = new order($oID);
//var_dump($order);
// prepare order-status pulldown list
$orders_statuses = array();
$orders_status_array = array();
$orders_status = $db->Execute("select orders_status_id, orders_status_name
                               from " . TABLE_ORDERS_STATUS . "
                               where language_id = '" . (int)$_SESSION['languages_id'] . "'");
while (!$orders_status->EOF) {
  $orders_statuses[] = array('id' => $orders_status->fields['orders_status_id'],
                             'text' => $orders_status->fields['orders_status_name'] . ' [' . $orders_status->fields['orders_status_id'] . ']');
  $orders_status_array[$orders_status->fields['orders_status_id']] = $orders_status->fields['orders_status_name'];
  $orders_status->MoveNext();
}

if($orderStatus == '4'){
	$orders_comments = $db->Execute("select h.comments
			from " . TABLE_ORDERS_STATUS_HISTORY . " h
			where h.orders_status_id = '4' and h.orders_id = '" . $oID . "'
			order by h.date_added desc
			limit 0,1"
	);
	
	$comment = '';
	if(!$orders_comments->EOF)
	{
		$comment = $orders_comments->fields['comments'];
	}	
}

?>

<!-- body_text //-->
<input type="button" id="copy-first-<?php echo $oID;?>" title="Copy First Column" class="copy-first" orderid="<?php echo $oID;?>" value="Copy First Column" />
<input type="button" id="copy-remain-<?php echo $oID;?>" title="Copy Remain Data" class="copy-remain" orderid="<?php echo $oID;?>" value="Copy Remain Data" />
<?php if ($orderStatus == '4')
{
	?>
	<input type="button" id="entregue-<?php echo $oID;?>" class="entregue" orderid="<?php echo $oID;?>" value="Entregue" />
	<?php 
}?>
<span id="lb-<?php echo $oID;?>" style="font-style: italic; color: red"></span>
<table border="0" width="100%" cellspacing="0" cellpadding="2" id="tbl_orderid_<?php echo $oID;?>">	
    <thead class="dataTableHeadingRow">
        <tr>
            <td class="dataTableHeadingContent" WIDTH="150">Artigo</td>
            <td class="dataTableHeadingContent" WIDTH="600">Descrição</td>    
            <td>&nbsp;</td><td>&nbsp;</td>
            <td class="dataTableHeadingContent">IVA</td>
            <td class="dataTableHeadingContent">IVA Ded.</td>
            <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
            <td class="dataTableHeadingContent">Pr. Unit</td>
            <td class="dataTableHeadingContent">Desc.</td>
            <td class="dataTableHeadingContent">UN</td>
            <td class="dataTableHeadingContent" align="right">Quant.</td>
        </tr>
    </thead>
     <tr>
     	<td class="dataTableContent">REFES</td>
     	<td class="dataTableContent"><?php echo $oID;?></td>   
        <td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">21</td>
     	<td class="dataTableContent">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">UN</td>
     	<td class="dataTableContent" align="right">0</td>
     </tr>
     <tr>
     	<td class="dataTableContent">PAGES</td>
     	<td class="dataTableContent">
     		<?php echo ($orderStatus == '2') ? $order->info['payment_method'] : $comment;?></td>  
        <td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">21</td>
     	<td class="dataTableContent">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">UN</td>
     	<td class="dataTableContent" align="right">0</td>
     </tr>
     <?php if ($orderStatus == '2')
	{
		?>
     <tr>
     	<td class="dataTableContent">NOMEES</td>
     	<td class="dataTableContent"><?php echo $order->customer['name'];?></td>
        <td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">21</td>
     	<td class="dataTableContent">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">UN</td>
     	<td class="dataTableContent" align="right">0</td>
     </tr>     
     <tr>
     	<td class="dataTableContent">MORADAES</td>
     	<td class="dataTableContent"><?php echo $order->delivery['street_address'] . ", " . $order->delivery['postcode'] . ", " . $order->delivery['city'];?></td>     	
        <td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">21</td>
     	<td class="dataTableContent">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">UN</td>
     	<td class="dataTableContent" align="right">0</td>
     </tr>
     <?php }?>
     <tr><td><br /></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<?php
for ($i = 0, $n = sizeof($order->products); $i < $n; $i++)
{
	$product = $order->products[$i];	
	//var_dump($product);
	?>
	<tr>
     	<td class="dataTableContent" valign="top"><?php echo $product['model'] . "ES";?></td>
     	<td class="dataTableContent">
     		<?php echo $product['name'];?>
     	</td>     	     	
        <td>&nbsp;</td><td>&nbsp;</td>
        <?php 
            $product_master_query = $db->Execute("select master_categories_id from " . TABLE_PRODUCTS . " where products_id=" . $product["id"]);
            if(!$product_master_query->EOF){
                if($product_master_query->fields['master_categories_id'] == 146 || $product_master_query->fields['master_categories_id'] == 102 || $product_master_query->fields['master_categories_id'] == 184 || $product_master_query->fields['master_categories_id'] == 183 || $product_master_query->fields['master_categories_id'] == 185
                    || $product_master_query->fields['master_categories_id'] == 121 || $product_master_query->fields['master_categories_id'] == 122) { ?>
        <td class="dataTableContent" valign="top">21</td>
     	<td class="dataTableContent" valign="top">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent" valign="top"><?php echo round(((double)$product['price'] * (1 + (double)($product['tax']/100))) / 1.21 , 2 );?></td>
        <?php }
        else {?>
        <td class="dataTableContent" valign="top">10</td>
     	<td class="dataTableContent" valign="top">10</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
        <td class="dataTableContent" valign="top"><?php echo round(((double)$product['price'] * (1 + (double)($product['tax']/100))) / 1.10 , 2 );?></td>
        <?php }        
        }
        else {?>
        <td class="dataTableContent" valign="top">10</td>
     	<td class="dataTableContent" valign="top">10</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
        <td class="dataTableContent" valign="top"><?php echo round(((double)$product['price'] * (1 + (double)($product['tax']/100))) / 1.10 , 2 );?></td>
        <?php }?>
     	<td class="dataTableContent" valign="top">0</td>
     	<td class="dataTableContent" valign="top">UN</td>
     	<td class="dataTableContent" valign="top" align="right"><?php echo $product['qty']?></td>
     </tr>
     <?php 
     if (sizeof($product['attributes']) > 0) {
     	for($j = 0; $j < sizeof($product['attributes']); $j++)
     	{
     		echo "<tr><td><br /></td><td>" . $product['attributes'][$j]['value'] . "</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
     	}     			
     }?>     
	<?php 	
}
?>
<?php if ($orderStatus == '2')
	{
		?>
	<tr>
     	<td class="dataTableContent"><?php if ( str_replace ("&euro;","",$order->totals[1]['text']) + 1 == 1 ) { echo 'TRPGRATISES' ; } else { echo 'TRPES'; } ?></td>
     	<td class="dataTableContent"><?php if ( str_replace ("&euro;","",$order->totals[1]['text']) + 1 == 1 ) { echo 'Envio Gratis' ; } else { echo 'Envio ' . $order->info['shipping_method']; } ?></td>     	
        <td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">21</td>
     	<td class="dataTableContent">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent"><?php echo round ( str_replace ("&euro;","",$order->totals[1]['text']) / 1.21 , 2 ) ; ?></td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">UN</td>
     	<td class="dataTableContent" align="right">1</td>
     </tr>
     <?php if($order->info['payment_module_code'] == 'cod') {?>
     <tr>
     	<td class="dataTableContent">COBES</td>
     	<td class="dataTableContent">Taxa de cobrança</td> 
        <td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">21</td>
     	<td class="dataTableContent">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent"><?php echo round ( 2.8 / 1.21 , 2 ) ; ?></td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">UN</td>
     	<td class="dataTableContent" align="right">1</td>
     </tr>
     <?php }?>
     <?php }?>
     <?php
     		$orders_history = $db->Execute("select orders_status_id, date_added, customer_notified, comments
						     				from " . TABLE_ORDERS_STATUS_HISTORY . "
						     				where orders_id = '" . zen_db_input($oID) . "'
						     				order by date_added
     										limit 0, 1");
     		if (!$orders_history->EOF)     		
     			if($orders_history->fields['comments'] != '')
     		{
     ?>	
	 <tr><td><br /></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
	 <?php if ($orderStatus == '2')
	{
		?>
     <tr>
     	<td class="dataTableContent">OBSES</td>
     	<td class="dataTableContent">
     		<?php echo $orders_history->fields['comments'];?>
     	</td>     	
        <td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">21</td>
     	<td class="dataTableContent">21</td>
        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">0</td>
     	<td class="dataTableContent">UN</td>
     	<td class="dataTableContent" align="right">0</td>
     </tr>
     <?php }?>     				
	<?php 
     		}
     ?>
     <tfoot>
	     <tr>
	     	<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
	     	<td class="dataTableContent" align="right"><strong>Total: </strong></td>
	     	<td class="dataTableContent" align="right"><strong><?php echo $order->info['total'];?></strong></td>
	     </tr>
     </tfoot>
</table><p></p>
<!-- body_text_eof //-->
<?php if (ORDER_COMMENTS_INVOICE > 0) { ?>
      <table>

<?php
    $orders_history = $db->Execute("select orders_status_id, date_added, customer_notified, comments
                                    from " . TABLE_ORDERS_STATUS_HISTORY . "
                                    where orders_id = '" . zen_db_input($oID) . "'
                                    order by date_added");

    if ($orders_history->RecordCount() > 0) {
      $count_comments=0;
      while (!$orders_history->EOF) {
        $count_comments++;
        echo '          <tr>' . "\n" .
             '            <td class="smallText" align="center">' . zen_datetime_short($orders_history->fields['date_added']) . '</td>' . "\n" .
             '            <td class="smallText" align="center">';
        if ($orders_history->fields['customer_notified'] == '1') {
          echo zen_image(DIR_WS_ICONS . 'tick.gif', ICON_TICK) . "</td>\n";
        } else {
          echo zen_image(DIR_WS_ICONS . 'cross.gif', ICON_CROSS) . "</td>\n";
        }
        echo '            <td class="smallText">' . $orders_status_array[$orders_history->fields['orders_status_id']] . '</td>' . "\n";
        echo '            <td class="smallText">' . ($orders_history->fields['comments'] == '' ? TEXT_NONE : nl2br(zen_db_output($orders_history->fields['comments']))) . '&nbsp;</td>' . "\n" .
             '          </tr>' . "\n";
        $orders_history->MoveNext();
        if (ORDER_COMMENTS_INVOICE == 1 && $count_comments >= 1) {
          break;
        }
      }
    } else {
        echo '          <tr>' . "\n" .
             '            <td class="smallText" colspan="5">' . TEXT_NO_ORDER_HISTORY . '</td>' . "\n" .
             '          </tr>' . "\n";
    }
?>
        </table><br /><p></p><hr /><br />
<?php } // order comments ?>
<?php

 	$pending_orders->MoveNext();
}
?>